<?php
class Admin extends CI_Controller {
	function __construct()
	{
		parent::__construct();
		$this->load->helper("url");
		$this->load->helper('form');
	}
	function index()
	{
		$sta = $this->session->userdata('logined');
		if (!isset($sta) || $sta!=='ok' ){
			redirect('admin/login');
		}
		$this->load->view("admin/index");
	}
	function top()
	{
		$data["loginuser"] = $this->session->userdata('loginuser');
		$this->load->view("admin/top",$data);
	}
	function center()
	{
		$this->load->view("admin/center");
	}
	function left()
	{
		$this->load->view("admin/left");
	}
	function right()
	{
		$this->load->view("admin/right");
	}
	function bottom()
	{
		$this->load->view("admin/bottom");
	}
	
	//==============================后台用户管理======================================================================
	function add_user()
	{
		$data["result_txt"] = "";
		$this->load->model('Madmin');
		$this->load->view("admin/add_user",$data);
	}
	
	function add_user_ok()
	{
		if ( $this->input->post('submit_user') ) 
		{
			$this->load->model('Madmin');
			if ( $this->Madmin->get_user_nums($this->input->post('username')) == 0  )
			{
				$query = $this->Madmin->insert_user();
				if ($query)
				{
					$data["result_txt"] =  "注册成功";
				} else {
					$data["result_txt"] = "注册失败";
				}
			} else {
				$data["result_txt"] = "用户名已存在，请重新添加";
			}
		}
		$this->load->view('admin/add_user',$data);				
	}
	
	function user_list()
	{
		$this->load->model('Madmin');
		$data["user_list"] = $this->Madmin->get_user_list();
		$this->load->view("admin/user_list",$data);
	}
	
	function del_user()
	{
		$this->load->model("Madmin");
		$this->Madmin->del_user($this->uri->segment(3));
		$this->user_list();
	}
	
	//================课件管理============================================
	
	function courseware_list()
	{
		$this->load->model("Madmin");
		$data["courseware_list"] = $this->Madmin->get_courseware_list();
		$this->load->view("admin/courseware_list",$data);
	}
	
	function add_courseware()
	{
		$this->load->model("Madmin");
		$data["category_list"] = $this->Madmin->get_category();
		$this->load->view("admin/add_courseware",$data);
	}
	
	function add_courseware_ok()
	{
		if ($this->input->post("submit_courseware"))
		{
			$config['upload_path']         = $this->config->item('upload_path');   //上传文件目录
			$config['allowed_types']	   ="gif|jpg|flv|rar";
			$config['max_size']			   = '20000000';
			$config['file_name']           = "cw_".date('YmdHis');
			$this->load->library('Upload', $config);
			if ($this->upload->do_upload("course_file"))
			{
				$data = array('upload_data'=>$this->upload->data());
				//var_dump($data);
				$dbdata["course_filename"] = $data["upload_data"]["file_name"];
			} else {
				$error = array('error'=>$this->upload->display_errors());
				echo "error=".$error;
				var_dump($error);
			}

			if ($this->upload->do_upload('attachment'))
			{
				$data = array('upload_data'=>$this->upload->data());
				//var_dump($data);
				$dbdata["attachment_filename"] = $data["upload_data"]["file_name"];
			} else {
				$error = array('error'=>$this->upload->display_errors());
				var_dump($error);
			}
			
			$this->load->model("Madmin");
			$query = $this->Madmin->insert_courseware($dbdata);
			
			if ($query)
			{
				$data["result_txt"] =  "添加成功";
			} else {
				$data["result_txt"] = "添加失败";
			}
			
			$this->load->view("admin/add_courseware",$data);
		}
	}
	
	function del_courseware()
	{
		$this->load->model("Madmin");
		$this->Madmin->del_courseware($this->uri->segment(3));
		$this->courseware_list();
	}
	
	//============================登录界面============================
	function login() 
	{
		$this->load->view("admin/login");
	}
	
	function check_login()
	{
		if($this->input->post("submit_login"))
		{
			$this->load->model("Madmin");
			$query = $this->Madmin->check_login();
			if ($query)
			{
				$this->session->set_userdata('logined','ok');
				$this->session->set_userdata('loginuser',$this->input->post('username'));
				redirect('admin/index');				
			} else {
				//$this->session->unset_userdata('logined','ok');
				$data["result_txt"] = "请检查，用户名密码错误";
				//redirect('admin/login',$data);
				$this->load->view("admin/login",$data);
			}
		}
	}
	
	function logout()
	{
		$this->session->sess_destroy();
		$this->load->view("admin/login");
	}
	
	//=======================================测试管理===========================================================
	function add_exam()
	{
		$this->load->view('admin/add_exam');
	}
	
	function exam_list()
	{
		$this->load->model('Madmin');
		$data["exam_list"] = $this->Madmin->get_exam($this->uri->segment(3));
		$this->load->view('admin/exam_list',$data);
	}
	
	function add_exam_ok()
	{
		$this->load->model("Madmin");
		$query = $this->Madmin->add_exam();
		if ($query)
		{
			$data["result_txt"] = "添加成功";
		} else {
			$data["result_txt"] = "添加失败";
		}
		$this->load->view('admin/add_exam',$data);
	}
	
	function del_exam()
	{
		$this->load->model("Madmin");
		$query = $this->Madmin->del_exam($this->uri->segment(4));
		$this->exam_list();
	}
	
	//===========================类别管理======================================================

	function add_category()
	{
		$this->load->view('admin/add_category');
	}
	
	function add_category_ok()
	{
		$this->load->model("Madmin");
		$query = $this->Madmin->add_category();
		if ($query)
		{
			$data["result_txt"] = "添加成功";
		} else {
			$data["result_txt"] = "添加失败";
		}
		$this->load->view('admin/add_category',$data);
	}
	
	function category_list()
	{
		$this->load->model("Madmin");
		$data["category_list"] = $this->Madmin->get_category();
		$this->load->view('admin/category_list',$data);
		
	}
	
	//=======================回家作业管理=======================================
	function homework_list()
	{
		$this->load->model("Madmin");
		$data["homework_list"] = $this->Madmin->get_homework_list($this->uri->segment(3));
		$this->load->view("admin/homework_list",$data);
	}
	
	function add_homework()
	{
		$this->load->view("admin/add_homework");
	}
	
	function add_homework_ok()
	{
		if ($this->input->post("submit_homework"))
		{
			$config['upload_path']         = $this->config->item('upload_path');   //上传文件目录
			$config['allowed_types']	   ="gif|docx|xls|rar|doc";
			$config['max_size']			   = '20000000';
			$config['file_name']           = "hw_".date('YmdHis');
			$this->load->library('Upload', $config);
			if ($this->upload->do_upload("homework_file"))
			{
				$data = array('upload_data'=>$this->upload->data());
				//var_dump($data);
				$dbdata["homework_filename"] = $data["upload_data"]["file_name"];
			} else {
				$error = array('error'=>$this->upload->display_errors());
				echo "error=".$error;
				var_dump($error);
			}
			
			$dbdata["rightnum"] = $this->input->post("rightnum");
			$dbdata["coursewareid"] = $this->input->post("coursewareid");
			$this->load->model("Madmin");
			$query = $this->Madmin->insert_homework($dbdata);
				
			if ($query)
			{
				$data["result_txt"] =  "添加成功";
			} else {
				$data["result_txt"] = "添加失败";
			}
				
			$this->load->view("admin/add_homework",$data);
		}		
	}
	
	function del_homework()
	{
		$this->load->model("Madmin");
		$query = $this->Madmin->del_homework($this->uri->segment(4));
		$this->homework_list();
	}
	
	
}
